package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpSellerStoreProductInfringingKeywordsExceptions;
import com.erp.gather.domain.bo.ErpSellerStoreProductInfringingKeywordsExceptionsBo;
import com.erp.gather.domain.vo.ErpSellerStoreProductInfringingKeywordsExceptionsVo;
import com.erp.gather.mapper.ErpSellerStoreProductInfringingKeywordsExceptionsMapper;
import com.erp.gather.service.IErpSellerStoreProductInfringingKeywordsExceptionsService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品侵权词例外情况Service业务层处理
 *
 * @author erp
 * @date 2023-12-06
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreProductInfringingKeywordsExceptionsServiceImpl implements IErpSellerStoreProductInfringingKeywordsExceptionsService {

    private final ErpSellerStoreProductInfringingKeywordsExceptionsMapper baseMapper;

    /**
     * 查询商品侵权词例外情况
     */
    @Override
    public ErpSellerStoreProductInfringingKeywordsExceptionsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品侵权词例外情况列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreProductInfringingKeywordsExceptionsVo> queryPageList(ErpSellerStoreProductInfringingKeywordsExceptionsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsExceptions> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreProductInfringingKeywordsExceptionsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品侵权词例外情况列表
     */
    @Override
    public List<ErpSellerStoreProductInfringingKeywordsExceptionsVo> queryList(ErpSellerStoreProductInfringingKeywordsExceptionsBo bo) {
        LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsExceptions> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsExceptions> buildQueryWrapper(ErpSellerStoreProductInfringingKeywordsExceptionsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsExceptions> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getKeywordsId() != null, ErpSellerStoreProductInfringingKeywordsExceptions::getKeywordsId, bo.getKeywordsId());
        lqw.like(StringUtils.isNotBlank(bo.getExceptionsClause()), ErpSellerStoreProductInfringingKeywordsExceptions::getExceptionsClause, bo.getExceptionsClause());
        lqw.eq(bo.getStatus() != null, ErpSellerStoreProductInfringingKeywordsExceptions::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreProductInfringingKeywordsExceptions::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增商品侵权词例外情况
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreProductInfringingKeywordsExceptionsBo bo) {
        ErpSellerStoreProductInfringingKeywordsExceptions add = BeanUtil.toBean(bo, ErpSellerStoreProductInfringingKeywordsExceptions.class);
        validEntityBeforeSave(add);
        add.setStatus(0);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品侵权词例外情况
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreProductInfringingKeywordsExceptionsBo bo) {
        ErpSellerStoreProductInfringingKeywordsExceptions update = BeanUtil.toBean(bo, ErpSellerStoreProductInfringingKeywordsExceptions.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreProductInfringingKeywordsExceptions entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品侵权词例外情况
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> setKeyWordsExceptionsStatus(Long exceptionsId, Integer type) {
        ErpSellerStoreProductInfringingKeywordsExceptions exceptions = baseMapper.selectById(exceptionsId);
        if (ObjectUtils.isEmpty(exceptions)){
            return R.fail("关键词例外情况不存在");
        }
        if (type == 1){
            //启用
            exceptions.setStatus(0);
        }else {
            //停用
            exceptions.setStatus(-1);
        }
        if (baseMapper.updateById(exceptions)>0){
            return R.ok("设置成功");
        }else {
            return R.fail("设置失败");
        }
    }
}
